package com.coustemview.qianfeng.huaqiangbeistore.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.Toast;

import com.coustemview.qianfeng.huaqiangbeistore.R;
import com.coustemview.qianfeng.huaqiangbeistore.adapter.MySpecialofferAdapter;
import com.coustemview.qianfeng.huaqiangbeistore.config.Netconfig;
import com.coustemview.qianfeng.huaqiangbeistore.model.GoodsBean;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by mingma on 2016/8/29.
 */
public class Fragment_mainpage_specialoffer extends Fragment {
    private ListView mListView;
    private LinearLayout mLinearLayout;
    //private List<SpecialOfferBean.DataBean> mData;
    private List<GoodsBean> mData;
    private MySpecialofferAdapter mAdapter;
    private RadioButton mRadioButtonToday, mRadioButtonFinal, mRadioButtonWill, mRadioButtonT, mRadioButtonF, mRadioButtonW;
    private List<Integer> listOne, listTwo;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mainpage_specialoffer, null);
        initView(view);
        initData();
        setData();
        setListener();
        return view;
    }

    private void initView(View view) {
        mRadioButtonToday = (RadioButton) view.findViewById(R.id.rb_specialoffer_today);
        mRadioButtonFinal = (RadioButton) view.findViewById(R.id.rb_specialoffer_final_sale);
        mRadioButtonWill = (RadioButton) view.findViewById(R.id.rb_specialoffer_will);
        mListView = (ListView) view.findViewById(R.id.lv_fragment_mainpage_specialoffer);
        mLinearLayout = (LinearLayout) view.findViewById(R.id.ll_fragment_specialoffer_floating);
        mListView.addHeaderView(LayoutInflater.from(getActivity()).inflate(R.layout.specialoffer_header_view, null));
        View itemView = LayoutInflater.from(getActivity()).inflate(R.layout.layout_specialoffer_floating, null);
        mListView.addHeaderView(itemView);
        mRadioButtonT = (RadioButton) itemView.findViewById(R.id.rb_specialoffer_today);
        mRadioButtonF = (RadioButton) itemView.findViewById(R.id.rb_specialoffer_final_sale);
        mRadioButtonW = (RadioButton) itemView.findViewById(R.id.rb_specialoffer_will);
    }

    private void initData() {
        mData = new ArrayList<>();
        mAdapter = new MySpecialofferAdapter(getActivity(), mData);
        x.http().get(new RequestParams(Netconfig.SPECIAL_OFFER_PATH), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.d("tag", "onSuccess+result: " + result);
//                Gson gson = new Gson();
//                SpecialOfferBean specialOfferBean = gson.fromJson(result, SpecialOfferBean.class);
//                List<SpecialOfferBean.DataBean> data = specialOfferBean.getData();
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    JSONArray data = jsonObject.getJSONArray("data");
                    for (int i = 0; i < data.length(); i++) {
                        GoodsBean bean = new GoodsBean();
                        JSONObject jsonObject1 = data.getJSONObject(i);
                        bean.buyEndTime = jsonObject1.getLong("buyEndTime");
                        bean.title = (String) jsonObject1.get("title");
                        bean.panicBuyingPrice = jsonObject1.getInt("panicBuyingPrice");
                        bean.imageMd5 = (String) jsonObject1.get("imageMd5");
                        bean.buyStartTime = (long) jsonObject1.get("buyStartTime");
                        bean.price = jsonObject1.getInt("price");
                        bean.hotSpot = (String) jsonObject1.get("hotSpot");
                        mData.add(bean);
                    }
                    listOne = new ArrayList<Integer>();
                    listTwo = new ArrayList<Integer>();
                    for (int i = 0; i < mData.size(); i++) {
                        long currentTime = System.currentTimeMillis();
                        long buyStartTime = mData.get(i).buyStartTime;
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        String start = sdf.format(buyStartTime);
                        String current = sdf.format(currentTime);
                        if (!start.equals(current)) {
                            listOne.add(i);
                        }
                        if (buyStartTime > currentTime) {
                            listTwo.add(i);
                        }
                    }
                    Log.d("tag", "onSuccess: " + mData);
                    mAdapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.d("tag", "wrong");
                Toast.makeText(getActivity(), "网络故障", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void setData() {
        mListView.setAdapter(mAdapter);
    }

    private void setListener() {
        mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (firstVisibleItem >= 1) {
                    mLinearLayout.setVisibility(View.VISIBLE);
                } else {
                    mLinearLayout.setVisibility(View.GONE);
                }
            }
        });
        mRadioButtonToday.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    // TODO: 2016/8/31
                    mListView.setSelectionFromTop(2, 0);
                    mRadioButtonToday.setTextColor(Color.WHITE);
                    mRadioButtonT.setChecked(true);
                } else {
                    mRadioButtonToday.setTextColor(Color.rgb(64, 125, 217));
                    mRadioButtonT.setChecked(false);
                }
            }
        });
        mRadioButtonFinal.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    if (listOne != null && listOne.size() > 0) {
                        mListView.setSelectionFromTop(listOne.get(0) + 2, 0);
                    }
                    mRadioButtonFinal.setTextColor(Color.WHITE);
                    mRadioButtonF.setChecked(true);
                } else {
                    mRadioButtonFinal.setTextColor(Color.rgb(64, 125, 217));
                    mRadioButtonF.setChecked(false);
                }
            }
        });
        mRadioButtonWill.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    if (listTwo != null && listTwo.size() > 0) {
//                        mListView.setSelectionFromTop(listTwo.get(0), -getActivity().getWindowManager().getDefaultDisplay().getHeight() + 315);
//                        Toast.makeText(getActivity(), "" + listTwo.get(0), Toast.LENGTH_SHORT).show();
                        mListView.setSelection(listTwo.get(0) + 2);
                    }
                    mRadioButtonWill.setTextColor(Color.WHITE);
                    mRadioButtonW.setChecked(true);
                } else {
                    mRadioButtonWill.setTextColor(Color.rgb(64, 125, 217));
                    mRadioButtonW.setChecked(false);
                }
            }
        });

        /////////////////////////////////////////////
        mRadioButtonT.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    mListView.setSelection(2);
                    mRadioButtonT.setTextColor(Color.WHITE);
                    mRadioButtonToday.setChecked(true);
                } else {
                    mRadioButtonT.setTextColor(Color.rgb(64, 125, 217));
                    mRadioButtonToday.setChecked(false);
                }
            }
        });
        mRadioButtonF.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    if (listOne != null && listOne.size() > 0) {
                        mListView.setSelectionFromTop(listOne.get(0) + 2, 0);
                    }
                    mRadioButtonF.setTextColor(Color.WHITE);
                    mRadioButtonFinal.setChecked(true);
                } else {
                    mRadioButtonF.setTextColor(Color.rgb(64, 125, 217));
                    mRadioButtonFinal.setChecked(false);
                }
            }
        });
        mRadioButtonW.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    if (listTwo != null && listTwo.size() > 0) {
                        //  mListView.setSelectionFromTop(listTwo.get(0), mListView.getParent().);
                        mListView.setSelection(listTwo.get(2));
                        //      Toast.makeText(getActivity(), "" + listTwo.get(0), Toast.LENGTH_SHORT).show();
                    }
                    mRadioButtonW.setTextColor(Color.WHITE);
                    mRadioButtonWill.setChecked(true);
                } else {
                    mRadioButtonW.setTextColor(Color.rgb(64, 125, 217));
                    mRadioButtonWill.setChecked(false);
                }
            }
        });
    }

}
